package com.joeljhou.project.form;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;

/**
 * 组件定义
 */
@Data
@NoArgsConstructor
public class Component {

    private static Component instance;

    //懒汉式单例模式
    public static synchronized Component getInstance() {
        if (instance == null) {
            instance = new Component();
        }
        return instance;
    }

    /**
     * 父窗口
     */
    private JFrame jFrame;

    /**
     * 端口（下拉框）
     */
    private JComboBox portComboBox;

    /**
     * 拉力值（输入框）
     */
    private JTextField pullTextField;

    /**
     * 扣子型号
     */
    private JTextField buckleModelTextField;

    /**
     * 布原(mm)
     */
    private JTextField buYuanTextField;

    /**
     * 机型
     */
    private JTextField machineTypeTextField;

    /**
     * 审核人
     */
    private JTextField auditorTextField;

    /**
     * 检测人
     */
    private JTextField testerTextField;

    /**
     * 保存按钮（Menu/iconButton）
     */
    private JMenuItem saveMenu;
    private JButton saveIconButton;

    /**
     * 清除按钮（Menu/iconButton）
     */
    private JMenuItem clearMenu;
    private JButton clearIconButton;

    /**
     * 退出按钮
     */
    private JMenuItem exitMenu;

    /**
     * 开始按钮（Menu/iconButton）
     */
    private JMenuItem openMenu;
    private JButton openIconButton;

    /**
     * 停止按钮（Menu/iconButton）
     */
    private JMenuItem closeMenu;
    private JButton closeIconButton;

    /**
     * 统计表格
     */
    private JTable statisticsTable;

    /**
     * 数据展示文本域
     */
    public JTextPane dataArea;

    /**
     * OK/NG 选中未选中开关控制
     */
    public JToggleButton toggleBtn;

    /**
     * 操作日志
     */
    private JLabel operaLabel;

    public static void printLog(String log) {
        printLog(log, Color.RED);
    }

    public static void printLog(String log, Color color) {
        JLabel operaLabel = Component.getInstance().getOperaLabel();
        operaLabel.setText(log);
        operaLabel.setForeground(color);
    }

    public static void appendText(String text) {
        appendText(text, Color.BLACK);
    }

    @SneakyThrows
    public static void appendText(String text, Color color) {
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, color);
        JTextPane dataArea = getInstance().getDataArea();
        int end = dataArea.getDocument().getLength();
        dataArea.getStyledDocument().insertString(end, text, set);
    }

}
